package com.utf999.qiangmanger.dao.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Component;

import com.utf999.qiangmanger.dao.AbstractRelationalDBDAO;
import com.utf999.qiangmanger.dao.iface.ICommodityDAO;
import com.utf999.qiangmanger.entity.orm.CommodityEntity;
import com.utf999.qiangmanger.exception.BizException;

/**
 * @author yinwenjie
 *
 */
@Component("CommodityDAOImpl")
public class CommodityDAOImpl extends AbstractRelationalDBDAO<CommodityEntity> implements ICommodityDAO {

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.AbstractRelationalDBDAO#getEntityClass()
	 */
	@Override
	protected Class<CommodityEntity> getEntityClass() {
		return CommodityEntity.class;
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.ICommodityDAO#queryCommoditysByType(java.lang.String, java.lang.Integer)
	 */
	@Override
	public List<CommodityEntity> queryCommoditysByType(String typeid, Integer status) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("typeid", typeid);
		if(status != null) { 
			condition.put("status", status);
		} 
		
		return this.queryByHqlFile("ICommodityDAO.queryCommoditysByType", condition);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.ICommodityDAO#queryCommodityByCommodityName(java.lang.String, java.lang.String)
	 */
	@Override
	public CommodityEntity queryCommodityByCommodityName(String companyid, String commodityName) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("companyid", companyid);
		condition.put("commodityName", commodityName);
		
		List<CommodityEntity> commodifys = this.queryByHqlFile("ICommodityDAO.queryCommodityByCommodityName", condition);
		if(commodifys == null || commodifys.size() == 0) {
			return null;
		}
		
		return commodifys.get(0);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.ICommodityDAO#queryCommodityByCommodityCode(java.lang.String, java.lang.String)
	 */
	@Override
	public CommodityEntity queryCommodityByCommodityCode(String companyid, String commodityCode) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("companyid", companyid);
		condition.put("commodityCode", commodityCode);
		
		List<CommodityEntity> commodifys = this.queryByHqlFile("ICommodityDAO.queryCommodityByCommodityCode", condition);
		if(commodifys == null || commodifys.size() == 0) {
			return null;
		}
		
		return commodifys.get(0);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.ICommodityDAO#queryCommodityBySortName(java.lang.String, java.lang.String)
	 */
	@Override
	public CommodityEntity queryCommodityBySortName(String companyid, String sortName) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("companyid", companyid);
		condition.put("sortName", sortName);
		
		List<CommodityEntity> commodifys = this.queryByHqlFile("ICommodityDAO.queryCommodityBySortName", condition);
		if(commodifys == null || commodifys.size() == 0) {
			return null;
		}
		
		return commodifys.get(0);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.ICommodityDAO#queryCommodityLikeSortName(java.lang.String, java.lang.String)
	 */
	//TODO 没有优化SQL索引
	@Override
	public List<CommodityEntity> queryCommodityLikeSortName(String companyid , String likeName) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("companyid", companyid);
		condition.put("likeName", '%' + likeName + '%');
		
		List<CommodityEntity> commoditys = this.queryByHqlFile("ICommodityDAO.queryCommodityLikeSortName", condition);
		return commoditys;
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.ICommodityDAO#queryCommodityLikeCommodityName(java.lang.String, java.lang.String)
	 */
	//TODO 没有优化SQL索引
	@Override
	public List<CommodityEntity> queryCommodityLikeCommodityName(String companyid , String likeName) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("companyid", companyid);
		condition.put("likeName", '%' + likeName + '%');
		
		List<CommodityEntity> commoditys = this.queryByHqlFile("ICommodityDAO.queryCommodityLikeCommodityName", condition);
		return commoditys;
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.ICommodityDAO#queryCommodityByOrderinfo(java.lang.String)
	 */
	@Override
	public List<CommodityEntity> queryCommodityByOrderinfo( String orderinfoid) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("orderinfoid", orderinfoid);
		
		List<CommodityEntity> commoditys = this.queryByHqlFile("ICommodityDAO.queryCommodityLikeByOrderinfo", condition);
		return commoditys;
	}
}
